﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYRElectric.WirePlace;
using YJKRGeometry;

namespace HYRElectric.Equipment
{
	public partial class RectLayoutExForm : System.Windows.Forms.Form
	{
		public RectLayoutExForm(Document document)
		{
			this.InitializeComponent();
			this.doc = document;
		}

		private void RectLayoutExForm_Load(object sender, EventArgs e)
		{
			this.InitForm();
			this.ReadFormDataFromXml();
		}

		private void InitForm()
		{
			//base.Icon = Resource.YArchitechIcon;
			this.txtRowCount.MostValue = new MostValue(1.0, 100.0);
			this.txtColCount.MostValue = new MostValue(1.0, 100.0);
			this.txtMargin.MostValue = new MostValue(0.0, 1.0);
			this.txtRowAngle.MostValue = new MostValue(0.0, 360.0);
			this.txtAngle.MostValue = new MostValue(0.0, 360.0);
			this.cmbWireStyle.Items.Add("弧线");
			this.cmbWireStyle.Items.Add("倒角");
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\RectEx";
			xmlHelper.SelectNode(path);
			string attributeEx = xmlHelper.GetAttributeEx("RowCount", "3");
			this.txtRowCount.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("ColCount", "3");
			this.txtColCount.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("Margin", "0.5");
			this.txtMargin.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("RowAngle", "0");
			this.txtRowAngle.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("Angle", "0");
			this.txtAngle.Text = attributeEx;
			int num = Convert.ToInt32(xmlHelper.GetAttributeEx("DislocationWay", "0"));
			if (num == 0)
			{
				this.radioNone.Checked = true;
			}
			else if (num == 1)
			{
				this.radioRow.Checked = true;
			}
			else
			{
				this.radioCol.Checked = true;
			}
			num = Convert.ToInt32(xmlHelper.GetAttributeEx("LinkWay", "0"));
			if (num == 0)
			{
				this.radioNoneLine.Checked = true;
			}
			else if (num == 1)
			{
				this.radioRowLine.Checked = true;
			}
			else
			{
				this.radioColLine.Checked = true;
			}
			this.UpdateWireControl();
			num = Convert.ToInt32(xmlHelper.GetAttributeEx("WireStyle", "1"));
			this.cmbWireStyle.SelectedIndex = num;
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
			base.Location = new System.Drawing.Point(x, y);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\RectEx";
			xmlHelper.SelectNode(path);
			xmlHelper.SetAttribute("RowCount", this.RowCount.ToString());
			xmlHelper.SetAttribute("ColCount", this.ColCount.ToString());
			xmlHelper.SetAttribute("Margin", this.Margin.ToString());
			xmlHelper.SetAttribute("RowAngle", this.RowAngle.ToString());
			xmlHelper.SetAttribute("Angle", this.Angle.ToString());
			xmlHelper.SetAttribute("DislocationWay", this.DislocationWay.ToString());
			xmlHelper.SetAttribute("LinkWay", this.LinkLineWay.ToString());
			xmlHelper.SetAttribute("WireStyle", this.WireStyle.ToString());
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml("");
		}

		private void UpdateWireControl()
		{
			this.buttonWire.Enabled = !this.radioNoneLine.Checked;
			this.cmbWireStyle.Enabled = !this.radioNoneLine.Checked;
		}

		private void radioNoneLine_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateWireControl();
		}

		private void buttonWire_Click(object sender, EventArgs e)
		{
			new NewWireParamForm(this.doc).ShowDialog();
		}

		private void RectLayoutExForm_Deactivate(object sender, EventArgs e)
		{
			string text = this.txtRowCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtRowCount.Text = this.txtRowCount.OldValue;
			}
			text = this.txtRowCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtRowCount.Text = "3";
			}
			text = this.txtColCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtColCount.Text = this.txtColCount.OldValue;
			}
			text = this.txtColCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtColCount.Text = "3";
			}
			text = this.txtMargin.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtMargin.Text = this.txtMargin.OldValue;
			}
			text = this.txtMargin.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtMargin.Text = "0.5";
			}
			text = this.txtRowAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtRowAngle.Text = this.txtRowAngle.OldValue;
			}
			text = this.txtRowAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtRowAngle.Text = "0";
			}
			text = this.txtAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtAngle.Text = this.txtAngle.OldValue;
			}
			text = this.txtAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtAngle.Text = "0";
			}
		}

		private void RectLayoutExForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveFormDataToXml();
		}

		protected override void WndProc(ref Message msg)
		{
			if (msg.Msg == 274 && (int)msg.WParam == 61536)
			{
				base.Hide();
				return;
			}
			base.WndProc(ref msg);
		}

		public int RowCount
		{
			get
			{
				return Convert.ToInt32(this.txtRowCount.Text.Trim());
			}
		}

		public int ColCount
		{
			get
			{
				return Convert.ToInt32(this.txtColCount.Text.Trim());
			}
		}

		public new double Margin
		{
			get
			{
				return Convert.ToDouble(this.txtMargin.Text.Trim());
			}
		}

		public int RowAngle
		{
			get
			{
				return Convert.ToInt32(this.txtRowAngle.Text.Trim());
			}
		}

		public int Angle
		{
			get
			{
				return Convert.ToInt32(this.txtAngle.Text.Trim());
			}
		}

		public int DislocationWay
		{
			get
			{
				if (this.radioCol.Checked)
				{
					return 2;
				}
				if (this.radioRow.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		public int LinkLineWay
		{
			get
			{
				if (this.radioColLine.Checked)
				{
					return 2;
				}
				if (this.radioRowLine.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		public int WireStyle
		{
			get
			{
				return this.cmbWireStyle.SelectedIndex;
			}
		}

		public void UpdateData()
		{
			try
			{
				int num = int.Parse(this.txtRowCount.Text);
				if (num <= 0)
				{
					num = 3;
				}
				this.txtRowCount.Text = num.ToString();
			}
			catch
			{
				this.txtRowCount.Text = "3";
			}
			try
			{
				int num2 = int.Parse(this.txtColCount.Text);
				if (num2 <= 0)
				{
					num2 = 3;
				}
				this.txtColCount.Text = num2.ToString();
			}
			catch
			{
				this.txtColCount.Text = "3";
			}
			try
			{
				double num3 = double.Parse(this.txtMargin.Text);
				if (Geometry.LessThan(num3, 0.0))
				{
					num3 = 0.5;
				}
				else if (object.Equals(num3, 0.0) && (this.RowCount == 1 || this.ColCount == 1))
				{
					num3 = 0.5;
				}
				this.txtMargin.Text = num3.ToString();
			}
			catch
			{
				this.txtMargin.Text = "0.5";
			}
			try
			{
				int num4 = int.Parse(this.txtRowAngle.Text);
				this.txtRowAngle.Text = num4.ToString();
			}
			catch
			{
				this.txtRowAngle.Text = "0";
			}
			try
			{
				int num5 = int.Parse(this.txtAngle.Text);
				this.txtAngle.Text = num5.ToString();
			}
			catch
			{
				this.txtAngle.Text = "0";
			}
		}

		private const string XmlNode_Root = "RectEx";

		private const string XmlNode_RowCount = "RowCount";

		private const string XmlNode_ColCount = "ColCount";

		private const string XmlNode_Margin = "Margin";

		private const string XmlNode_RowAngle = "RowAngle";

		private const string XmlNode_Angle = "Angle";

		private const string XmlNode_DislocationWay = "DislocationWay";

		private const string XmlNode_LinkWay = "LinkWay";

		private const string XmlNode_WireStyle = "WireStyle";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private Document doc;
	}
}
